package com.example.demo.config;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class UserRealm  extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权-------------------------------------");
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        //可以做数据库具体操作
        info.addStringPermission("user:add");
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("认证-------------------------------------");
        String user="root";
        String pwd ="123456";
        //可以做数据库具体操作
        UsernamePasswordToken userToken= (UsernamePasswordToken) authenticationToken;
        if (!userToken.getUsername().equals(user)){
            return null;
        }

        return new SimpleAuthenticationInfo("",pwd,"");
    }
}
